package com.leyou.item.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.exception.LyException;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.service.SpecGroupService;
import com.leyou.item.service.SpecParamService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SpecGroupServiceImpl extends ServiceImpl<SpecGroupMapper, SpecGroup> implements SpecGroupService {
    @Resource
    private SpecParamService specParamService;

    @Override
    public List<SpecGroupDTO> querySpecGroupByCategoryId(Long id) {
        return SpecGroupDTO.convertEntityList(query().eq("category_id",id).list());
    }

    @Override
    public void updateGroup(SpecGroupDTO specGroupDTO) {
        SpecGroup specGroup = specGroupDTO.toEntity(SpecGroup.class);
        updateById(specGroup);
    }

    @Override
    public List<SpecGroupDTO> querySpecList(Long categoryId) {
        List<SpecGroupDTO> groupDTOS = SpecGroupDTO.convertEntityList(query().eq("category_id", categoryId).list());
        if (CollectionUtils.isEmpty(groupDTOS)){
            throw new LyException(404, "该分类下的规格组不存在！");
        }

        for (SpecGroupDTO specGroupDTO : groupDTOS) {
            specGroupDTO.setParams(specParamService.queryParams(categoryId, specGroupDTO.getId(), null));
        }
        return groupDTOS;
    }
}
